import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

@Injectable()
export class PostsService {
    private lpfUrl = 'http://localhost:3662';
    //private lpfUrl = 'http://lpf.arktao.com';

    constructor(private http: Http) { }

    //ApproveStatus(0:审核中,1:已审核，99:审核未通过，所有则为'') 
    //Publisher("official":官方，"private":私人,所有则为'')
    //Status(0:暂存,1:提交,99:删除，所有则为'')
    //Status(0:暂存,1:提交,99:删除，所有则为'')
    //获取帖子列表

    GetPostsList(page: number, limit: number, ApproveStatus: number, Publisher: string, Status: number, UserGuid: string): Promise<any> {
        return this.http.get(this.lpfUrl + `/Posts/PostsList?page=${page}&limit=${limit}&ApproveStatus=${ApproveStatus}&Publisher=${Publisher}&Status=${Status}&UserGuid=${UserGuid}`)
            .toPromise().then(res => { return res.json() })
    }
    //获取帖子数量
    GetPostsCount(ApproveStatus: number, Publisher: string, Status: number, UserGuid: string): Promise<any> {
        return this.http.get(this.lpfUrl + `/Posts/PostsCount?ApproveStatus=${ApproveStatus}&Publisher=${Publisher}&Status=${Status}&UserGuid=${UserGuid}`)
            .toPromise().then(res => { return res.json() })
    }
    // //获取某人帖子列表
    // GetPostsListByUser(page: number, limit: number, ApproveStatus: number, Publisher: string, UserGuid: string, Status: number): Promise<any> {
    //     return this.http.get(this.lpfUrl + `/Posts/PostsListByUser?page=${page}&limit=${limit}&ApproveStatus=${ApproveStatus}&Publisher='${Publisher}'&UserGuid=${UserGuid}&Status=${Status}`)
    //         .toPromise().then(res => { return res.json() })
    // }
    // //获取某人帖子数量
    // GetPostsCountByUser(ApproveStatus: number, Publisher: string, UserGuid: string, Status: number): Promise<any> {
    //     return this.http.get(this.lpfUrl + `/Posts/PostsListByUser?ApproveStatus=${ApproveStatus}&Publisher=${Publisher}&UserGuid='${UserGuid}'&Status=${Status}`)
    //         .toPromise().then(res => { return res.json() })
    // }
    //获取某个帖子
    GetPosts(PostsGuid: string): Promise<any> {
        return this.http.get(this.lpfUrl + `/Posts/Posts/${PostsGuid}`)
            .toPromise().then(res => { return res.json() })
    }
    //增加帖子
    AddPosts(Title: string, CoverUrl: string, Content: string, Publisher: string, ApproveStatus: number, UserGuid: string, Status: number): Promise<any> {
        return this.http.post(this.lpfUrl + `/Posts/Posts`, {
            Title: Title,
            CoverUrl: CoverUrl,
            Content: Content,
            Publisher: Publisher,
            ApproveStatus: ApproveStatus,
            UserGuid: UserGuid,
            Status: Status,
        })
            .toPromise().then(res => { return res.json() })
    }
    //修改帖子
    EditPosts(PostsGuid: string, Title: string, CoverUrl: string, Content: string, Status: number): Promise<any> {
        return this.http.put(this.lpfUrl + `/Posts/Posts`, {
            PostsGuid: PostsGuid,
            Title: Title,
            CoverUrl: CoverUrl,
            Content: Content,
            Status: Status
        })
            .toPromise().then(res => { return res.json() })
    }
    //修改审核状态
    ApprovePosts(PostsGuid: string, Approver: string, ApproveStatus: string): Promise<any> {
        return this.http.put(this.lpfUrl + `/Posts/ApprovePosts`, {
            PostsGuid: PostsGuid,
            Approver: Approver,
            ApproveStatus: ApproveStatus,
        })
            .toPromise().then(res => { return res.json() })
    }
    //删除帖子
    DeletePosts(PostsGuid: string, UserGuid: string): Promise<any> {
        return this.http.delete(this.lpfUrl + `/Posts/Posts?PostsGuid=${PostsGuid}&UserGuid=${UserGuid}`)
            .toPromise().then(res => { return res.json() })
    }
    //增加浏览量
    AddPageView(PostsGuid: string): Promise<any> {
        return this.http.post(this.lpfUrl + `/Posts/AddPageView`, {
            PostsGuid: PostsGuid
        })
            .toPromise().then(res => { return res.json() })
    }
}